﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.ModelBinding;
using OUSMS.AppLogic.BusinessLogicLayer;
using OUSMS.AppLogic.DataAccessLayer;
using OUSMS.Helpers;

namespace OUSMS
{
    public partial class ToCheckUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Label studyAreaLbl = userInfoFormView.FindControl("studyAreaLbl") as Label;
                UsersBLL usersBLL = new UsersBLL();
                Users userEntity = usersBLL.GetUsersByID(Convert.ToInt64(Request.QueryString["UID"])).FirstOrDefault();

                foreach (UserStudyAreas userStudyArea in userEntity.UserStudyAreas) 
                {
                    studyAreaLbl.Text += userStudyArea.StudyAreas.Name + ";";
                }
            }
        }

        public IQueryable<Users> GetUsersByID([QueryString("UID")]long uid)
        {
            UsersBLL usersBLL = new UsersBLL();
            return usersBLL.GetUsersByID(uid);
        }

        protected void submitBtn_Click(object sender, EventArgs e)
        {
            try
            {
                UsersBLL usersBLL = new UsersBLL();
                string emailBody = "";
                Users userEntity = usersBLL.GetUsersByID(Convert.ToInt64(Request.QueryString["UID"])).FirstOrDefault();
                RadioButtonList approveResultRBL = userInfoFormView.FindControl("approveResultRBL") as RadioButtonList;
                userEntity.IsApproved = Convert.ToBoolean(approveResultRBL.SelectedValue);

                TextBox approveCommentTxtBox = userInfoFormView.FindControl("approveCommentTxtBox") as TextBox;
                userEntity.ApproveComment = !string.IsNullOrEmpty(approveCommentTxtBox.Text.Trim()) ? approveCommentTxtBox.Text.Trim() : approveResultRBL.SelectedItem.Text;
                userEntity.ApproveDateTime = DateTime.Now;
                Users currentUser = usersBLL.GetUserByLoginName(User.Identity.Name);
                userEntity.ApproverID = currentUser.ID;
                usersBLL.Save();

                if (userEntity.IsApproved.Value)
                {
                    emailBody = string.Format("{0}, 您好： 您的注册申请已审核通过，您现在可以登录系统提交科研提案。", userEntity.Name);
                }
                else
                {
                    emailBody = string.Format("{0}, 您好： 您的注册申请审核未通过，原因是'{1}'，您现在可以登录系统提交科研提案。", userEntity.Name, userEntity.ApproveComment);
                }
                
                Emailer.SendMail("国家开放大学科研提案系统审核通知", userEntity.Email, "libncepu@sina.com", emailBody);

                Response.Redirect("ToCheckUserList.aspx");
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                Label errorMsgLbl = userInfoFormView.FindControl("errorMsgLbl") as Label;
                errorMsgLbl.Text = string.Format("提交信息失败，原因：{0}，请联系管理员。", ex.Message);
                errorMsgLbl.Visible = true;
            }
        }
    }
}